import { GameEvent } from "../core/GameEvent";
import { GameData } from "../core/GameData";

export const LANG_CHANGE = 'langchange';

export enum Lang {
    cn,
    eng,
}

const { ccclass, property } = cc._decorator;

@ccclass
export default class Localization<T> extends cc.Component {

    @property({ type: cc.Enum(Lang) })
    protected spareLang: Lang = Lang.cn;

    protected onLoad() {
        GameEvent.on(LANG_CHANGE, this.changeLang, this);
    }

    protected changeLang() { }

    protected get(): T {
        let lang: string = null;
        if (this[GameData.lang]) {
            if (Array.isArray(this[GameData.lang]) && this[GameData.lang].length === 0) {
                lang = Lang[this.spareLang];
            } else {
                lang = GameData.lang;
            }
        } else {
            lang = Lang[this.spareLang];
        }
        return this[lang];
    }

    protected onDestroy() {
        GameEvent.off(LANG_CHANGE, this.changeLang, this);
    }
}
